package com.book.workbench.controller;

import com.book.common.Result;
import com.book.settings.pojo.User;
import com.book.workbench.pojo.Kind;
import com.book.workbench.service.KindService;
import com.github.pagehelper.PageInfo;
import jdk.jshell.Snippet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class KindController {

    @Autowired
    private KindService kindService;

    @PostMapping("/workbench/kind/add.do")
    public Result addKind(@RequestBody Kind kind){
        int addKindResult = kindService.addKind(kind);
        if(addKindResult==1){
            return Result.success();
        }else if(addKindResult==-1){
            return Result.error("添加的类型已经存在");
        }else{
            return Result.error("系统错误");
        }
    }

    @GetMapping("/workbench/kind/search.do")
    public Result searchKind( Kind kind){
        PageInfo<Kind>  kindPageInfo= kindService.getListKind(kind);
        int countListKind = kindService.getCountListKind(kind);
        return Result.successKinds(kindPageInfo.getList(),countListKind);
    }

    @PostMapping("/workbench/kind/del.do")
    public Result deleteKind(@RequestBody Kind kind){
        int deleteKindResult = kindService.deleteKindById(kind);
        if(deleteKindResult==1){
            return Result.success();
        } else if (deleteKindResult==-1) {
            return Result.error("存在该类型的书籍");
        }else{
            return Result.error("未找到要删除的类型");
        }
    }

    @PostMapping("/workbench/kind/update.do")
    public Result updateKind(@RequestBody Kind kind){
        int updateKindById = kindService.updateKindById(kind);
        if(updateKindById==1){
            return Result.success();
        }else{
            return Result.error("未找到要修改的类型");
        }
    }

    @GetMapping("/workbench/kind/searchall.do")
    public Result searchAllKind(Kind kind){
        List<Kind> kinds= kindService.getAllListKind();
        int countGetListAllKind = kindService.getCountListKind(kind);
        return Result.successKinds(kinds,countGetListAllKind);
    }

    @GetMapping("/workbench/kind/searchbyid.do")
    public Kind searchKindById(String id){
        Kind kind  = kindService.searchKindById(id);
        return kind;
    }

}
